package cn.edu.gsau.info.controller;


import cn.edu.gsau.info.models.*;
import cn.edu.gsau.info.myenum.ModuleIdClass;
import cn.edu.gsau.info.service.IJiaogaiService;
import cn.edu.gsau.info.service.IZhengmingcailiaoService;
import cn.edu.gsau.info.util.UploadUtil;
import cn.edu.gsau.info.vo.ParamModel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: JiaogaiController
 * @BelongsPackage: cn.edu.gsau.info.controller
 * @Author: kql
 * @CreateTime: 2022/11/18
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("/jiaogai")
@JsonSerialize(using = ToStringSerializer.class)
public class JiaogaiController {

    @Autowired
    IJiaogaiService jiaogaiService;

    @Autowired
    IZhengmingcailiaoService zhengMingCaiLiaoService;


    @Value("${gsau.upload.path}")
    private String uploadfile;
    /**
     * @Description 查询所有教改项目
     * @Param []
     * @return cn.edu.gsau.info.models.ResponseResult
     * @Date 2022/11/21 21:16
     * @Author kql
     **/

    @PostMapping("/findjiaogai")
    public ResponseResult findjiaogai(@RequestBody ParamModel paramModel){
        LoginUser loginUser=(LoginUser) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        User user = loginUser.getUser();
        IPage jiaogais = jiaogaiService.selectJiaoGai(user.getId(),paramModel);
        return new ResponseResult(Code.GET_OK,"success",jiaogais) ;
    }
    /**
     * @Description //TODO
     * @Param [paramModel]
     * @return com.baomidou.mybatisplus.core.metadata.IPage
     * @Date 2022/11/28 19:53
     * @Author kql
     **/
    @PostMapping("/shenpijiaogai")
    public ResponseResult shenpijiaogai(@RequestBody ParamModel paramModel){
        LoginUser loginUser=(LoginUser) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        User user = loginUser.getUser();
        IPage jiaogais = jiaogaiService.shenPiJiaoGai(paramModel);
        return new ResponseResult(Code.GET_OK,"success",jiaogais) ;
    }


    /**
     * @Description 通过项目id查询教改项目详情
     * @Param [id]
     * @return cn.edu.gsau.info.models.ResponseResult
     * @Date 2022/11/21 17:13
     * @Author kql
     **/
    @GetMapping("/searchbyjiaogaiid/{id}")
    public ResponseResult searchbyjiaogaiid(@PathVariable String id){
        Jiaogai jiaogai = jiaogaiService.searchbyjiaogaiid(id);
        return new ResponseResult(Code.GET_OK,"success",jiaogai);
    }


    /**
     * @Description //通过教改项目id获取认证材料
     * @Param [id]
     * @return cn.edu.gsau.info.models.ResponseResult
     * @Date 2022/11/21 17:33
     * @Author kql
     **/
    @GetMapping("/searchbymoduleidandrecoreid/{id}")
    public ResponseResult searchbymoduleidandrecoreid(@PathVariable long id){
        int jiaogai_id = ModuleIdClass.JIAOGAI_id;
        List<Zhengmingcailiao> list = zhengMingCaiLiaoService.searchbymoduleidandrecoreid(jiaogai_id, id);
        return new ResponseResult(Code.GET_OK,"success",list);
    }

    /**
     * @Description 通过主键删除数据
     * @Param [id]
     * @return cn.edu.gsau.info.models.ResponseResult
     * @Date 2022/11/21 20:07
     * @Author kql
     **/
    @DeleteMapping("/delete/{id}")
    public ResponseResult deleteByid(@PathVariable long id){
        boolean b = jiaogaiService.removeById(id);
        return new ResponseResult(Code.DELETE_OK,"success",b);
    }

    /**
     * @Description
     * @Param [file, request]
     * @return cn.edu.gsau.info.models.ResponseResult
     * @Date 2022/11/22 20:18
     * @Author kql
     **/
    @PostMapping("/upload")
    public ResponseResult upload(MultipartFile file){
        User user = new User();
        user.setUserName("kangqinglong");
        UploadUtil uploadUtil = new UploadUtil();
        Map<String, String> map = uploadUtil.filedUpload(file,user.getUserName(), uploadfile);
        return new ResponseResult(Code.SAVE_OK,"success",map);
    }

    /**
     * @Description 添加教改和认证材料
     * @Param [form]
     * @return cn.edu.gsau.info.models.ResponseResult
     * @Date 2022/11/23 17:04
     * @Author kql
     **/
    @PostMapping("/save")
    public ResponseResult save(@RequestBody Jiaogai form){
        LoginUser loginUser=(LoginUser) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        User user = loginUser.getUser();
        System.out.println(form.toString());
        int add = jiaogaiService.add(user,form);
        return  new ResponseResult(Code.SAVE_OK,"success",add);
    }
    /**
     * @Description 修改信息
     * @Param [form]
     * @return cn.edu.gsau.info.models.ResponseResult
     * @Date 2022/11/26 15:48
     * @Author kql
     **/
    @PutMapping("/edit")
    public ResponseResult edit(@RequestBody Jiaogai form){
        System.out.println(form.toString());
        int edit = jiaogaiService.edit(form);
        return new ResponseResult(Code.SAVE_OK,"success",edit);
    }

    /**
     * @Description //TODO
     * @Param [jiaogai]
     * @return cn.edu.gsau.info.models.ResponseResult
     * @Date 2022/11/29 16:02
     * @Author kql
     **/
    @PostMapping("/shenpi")
    public ResponseResult shenPi(@RequestBody Jiaogai jiaogai){
        int i = jiaogaiService.shenPi(jiaogai);
        return new ResponseResult(Code.SAVE_OK,"success",i);
    }
    /**
     * @Description //TODO
     * @Param [id]
     * @return cn.edu.gsau.info.models.ResponseResult
     * @Date 2022/11/29 20:02
     * @Author kql
     **/
    @GetMapping("/shenheluicheng/{id}")
    public ResponseResult shenheluicheng(@PathVariable long id){
        List<Shenhe> shenheluicheng = jiaogaiService.shenheluicheng(id);
        return new ResponseResult(Code.GET_OK,"success",shenheluicheng);
    }
}

